﻿@model RBAC.Web.Models.ListPermissionModel
@using RBAC.Web.Utilities
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper

@{
    ViewBag.Title = "List Permission";
    Layout = "~/Views/Shared/_Layout.cshtml";

    string result = Request.QueryString["result"];
    string operation = Request.QueryString["operation"];
    string messsage = "";
    bool? isSuccess = false;
    if (!String.IsNullOrEmpty(result) && !String.IsNullOrEmpty(operation))
    {
        messsage = MessageUtil.GetMessage(MessageUtil.GetOperationFromString(operation), Convert.ToBoolean(result));
        isSuccess = Convert.ToBoolean(result);
    }
    else
    {
        messsage = Model.Message;
        isSuccess = Model.IsSuccessed;

    }   
}

@section Styles {
    <!--<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />-->
    <script type="text/javascript">
        $(function () {
            $("#confirm-delete").click(function () {
                       
                $.post('@Url.Action("DeletePermissions", "Permission")',$("#main-form").serialize(), function(data) {
                    window.location.href = '@Url.Action("ListPermission", "Permission")' + '?result=' + data + '&operation=delete';
                        
                });
                   
            });

            $("#button-delete").click(function () {
              
                var checkboxes = $('.table input:checkbox');
                if(checkboxes.size() == 0)
                    $('#modalalert').modal(); 
                    
                else
                {
                    var isCheck = false;
                    for(i = 0; i < checkboxes.size(); i ++)
                    {
                        isCheck = checkboxes[i].checked;
                        if(isCheck)
                            break;
                    }

                    if(!isCheck){
                        
                        $('#modalalert').modal(); 
                        
                    }
                    else{
                       
                        $('#deleteItem').modal(); 
                      
                    }
                }
            });
        });

    </script>
}



<h3 class="page-header">Permission</h3>
@using (Html.BeginForm("ListPermission", "Permission", FormMethod.Post, new { @id = "main-form" }))
{
    //Verion 1.5
    <div class="panel panel-primary">
        <div class="panel-heading">Search </div>
        <div class="panel-body">

            <div class="form-group row">


                <div class="col-md-6">
                    <div class="col-md-4">
                        <label>Permission Name</label>
                    </div>

                    <div class="col-md-6">
                        @Html.TextBoxFor(model => model.PermissionNameSearch, new { @class = "form-control input-sm", placeholder = "Enter PermissionName" })
                    </div>

                    <div class="col-md-1">
                        <label>
                            <input class="btn btn-primary" type="submit" id="Search" value="Search" name="submitButton" /></label>
                    </div>



                </div>
            </div>

        </div>
    </div> 
    
    @Html.DisplayMessage(messsage, isSuccess)
    <input type="hidden" id="hid_delete" value="Delete" name="DeleteButton" />



    <div class="row">
        <div class="col-md-1">
            <label>
                <input class="btn btn-primary" id="add" type="button" value="New" onclick="location.href='@Url.Action("Add", "Permission")    '"/></label>
        </div>
        <div class="col-md-1">
            <label>
                <input class="btn btn-primary" type="button" value="Delete" id="button-delete" data-toggle="modal" /></label>
        </div>
    </div>



    <div class="modal" id="modalalert">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Delete Permission</h4>
                </div>
                <div class="modal-body">
                    No item was selected.
                </div>
                <div class="modal-footer">
                    <a href="#" data-dismiss="modal" class="btn btn-primary">Ok</a>

                </div>
            </div>
        </div>
    </div>


    <div class="modal" id="deleteItem">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Delete Permission</h4>
                </div>
                <div class="modal-body">
                    Do you want to delete?
                </div>
                <div class="modal-footer">
                    <button type="button" data-dismiss="modal" id="confirm-delete" class="btn btn-primary">YES</button>
                    <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
                </div>
            </div>
        </div>
    </div>



        

    //start grid
    if (Model.ListItems != null && Model.ListItems.Count > 0)
    {
        var grid = new WebGrid(
            source: Model.ListItems, canPage: false);

    <div class="panel panel-default">
        <div class="panel-heading">Permission List</div>
        <div class="panel-body">

            <div style="overflow: scroll; width: 100%; height: auto; max-height: 400px;">
                @grid.GetHtml(
        tableStyle: "table table-striped table-bordered table-hover dataTable no-footer grid",
        rowStyle: "row-a",
        alternatingRowStyle: "row-b",
        columns: grid.Columns(
       grid.Column(header: "", style: "width_5per", format: @<text><input name="PermissionCheckbox" type="checkbox" value="@item.Id" /></text>)


                    , grid.Column(header: "Permission Name", style: "width_100per", columnName: "PermissionName",
                    format: item =>
                    {
                        string link = "<a href=\"{0}?PermissionName={1}\" class=\"word_wrap\">{2}</a>";
                        return Html.Raw(string.Format(link, @Url.Content("~/Permission/EditPermission"), item.PermissionName, item.Value.PermissionName));
                    }
               )
             , grid.Column(header: "Description", canSort: false, style: "width_100per", columnName: "Description",
                 format: item =>
                 {
                     string Description = "<div class=\"word_wrap\">{0}</div>";
                     return Html.Raw(string.Format(Description, String.IsNullOrWhiteSpace(item.Description + "") ? "" : item.Value.Description));
                 }
              )

                    , grid.Column(header: "Create Date", canSort: false, style: "width_100per", columnName: "CreateDate",
                        format: item =>
                        {
                            string CreateDate = "<div class=\"word_wrap\">{0}</div>";
                            return Html.Raw(string.Format(CreateDate, DateTime.Parse(item.CreateDate.ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"))));
                        }
                    )

        ))
            </div>


        </div>
    </div>
    }
    else
    {
    <div class="alert alert-info alert-dismissable">
        No item base on search criteria that you had inputed. 
    </div>
    }
}





